// Package public 数据统计
package public

import (
	"encoding/csv"
	"fmt"
	"io/ioutil"
	"strconv"
	"strings"
)

//Mouse 统计鼠标点击
type Mouse struct {
	Date        string
	KeyBoard    int
	Distance    string
	LeftClicks  int
	MidClicks   int
	RightClicks int
	Uptime      string
}

//ExcelTool is tool helper
type ExcelTool struct {
	FileName string
}

//ReadCsvData is read
//bug1 Golang默认输出为UTF-8 对文件进行UTF-8编码
func (excel *ExcelTool) ReadCsvData() (bool, string) {
	if strings.Contains(excel.FileName, ".csv") == true || strings.Contains(excel.FileName, ".excel") == true {

	} else {
		fmt.Println("csv format err")
		return false, ""
	}

	cntb, err := ioutil.ReadFile(excel.FileName)
	if err != nil {
		fmt.Println("read faield", excel.FileName, err)
		return false, ""
	}
	// 读取文件数据
	records, _ := csv.NewReader(strings.NewReader(string(cntb))).ReadAll()

	// 循环取数据

	var index int = 0
	var maxValue int = 0
	var minValue int = 99999
	var totalValue int = 0
	totalMouse := make([]Mouse, 7)
	for i := len(records) - 1; i > 0; i-- {
		var mouse Mouse
		row := records[i]
		mouse.Date = row[0]
		mouse.KeyBoard, _ = strconv.Atoi(strings.Replace(row[1], " ", "", -1))
		mouse.Distance = row[2]
		mouse.LeftClicks, _ = strconv.Atoi(strings.Replace(row[3], " ", "", -1))

		mouse.MidClicks, _ = strconv.Atoi(strings.Replace(row[4], " ", "", -1))
		mouse.RightClicks, _ = strconv.Atoi(strings.Replace(row[5], " ", "", -1))
		if mouse.LeftClicks == 0 || mouse.KeyBoard == 0 {
			continue
		}
		totalMouse = append(totalMouse, mouse)
		//fmt.Println(mouse)
		index++
		//data
		maxValue = max(maxValue, mouse.LeftClicks)
		minValue = min(minValue, mouse.LeftClicks)
		totalValue += mouse.LeftClicks
		if index == 7 {
			break
		}

	}

	var msg string
	msg += WeekByToday()
	msg += "鼠标点击次数统计: \r\n"
	msg += "最大: "
	msg += strconv.Itoa(maxValue)
	msg += " 最小: "
	msg += strconv.Itoa(minValue)
	msg += " 平均: "
	msg += strconv.Itoa(totalValue / 7)
	msg += " 频率: "
	data := 28800 * 7 / totalValue
	msg += strconv.Itoa(data)
	msg += "\r\n"
	if data <= 6 {

		msg += strconv.Itoa(data)
		msg += "秒一次 "
		msg += "这说明这一周 缺乏思路，缺乏思考"
		msg += "\r\n"
		msg += "本周结果肯定是 冲动，疲劳 情绪不佳"

	} else if data > 6 && data <= 10 {
		msg += strconv.Itoa(data)
		msg += "秒一次"
		msg += "这说明这一周 只有努力，没有思考，停留在舒适区"
	} else {
		msg += strconv.Itoa(data)
		msg += "秒一次"
		msg += "这说明这一周状态很棒"
	}
	fmt.Println(msg)
	return true, msg
}

//max 最大
func max(a int, b int) int {
	if a > b {
		return a
	}
	return b
}

//min 最小
func min(a int, b int) int {
	if a > b {
		return b
	}
	return a
}
